﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;



public partial class Notice : System.Web.UI.Page
{
    int pageSize = 10;
    GetData gd = new GetData();

    string generateNotice(notices n)
    {
        string nickname = gd.GetOneMember(n.fromUid).nickname;
        int uid = n.fromUid;
        string topicname=gd.GetOneThread(n.tid).title;
        if (topicname.Length > 10)
            topicname = topicname.Substring(0, 10) + "...";
        int tid=n.tid;
        int page = 1;
        string content;
        if (n.pid == -1)
            content = gd.GetOneThread(n.tid).text;
        else
            content = gd.GetOnePosts(n.pid).text;
        if (content.Length > 10)
            content = content.Substring(0, 10) + "...";
        string reply = n.message;

        string R = "<tr><td>";
        R += "<a href=\"Personal.aspx?uid=" + uid + "\">" + nickname + "</a>&nbsp;";
        R += "曾在您所回复的主题&nbsp;<a href=\"ViewTopic.aspx?tid=" + tid + "&page=" + page + "\">" + topicname + "</a>&nbsp;中回复了您<br/>";
        R += "<dt>您的帖子:&nbsp;&nbsp;<span>" + content + "</span></dt>";
        R += "<dt><a href=\"Personal.aspx?uid=" + uid + "\">" + nickname + "</a>";
        R += "说:&nbsp;<span>" + Server.HtmlEncode(reply) + "</span></dt>";
        R+= "<a href=\"ReadNotice.aspx?type=notice&nid="+n.nid+"&goto="+n.address+"\">" + "查看" + "</a>";
        R += "</td></tr>";
        return R;

    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        CGeneral.CheckLogin(Request, Session);
        message.Visible = false;
        sysmessage.Visible = false;
        notice.Visible = false;
        string type;

        Permission per = new Permission(int.Parse(Session["gid"].ToString()));
        if(per.awaccess==0||per.awpost==0)
            CGeneral.GoToErrorPage(Response, "custom","YOU NEED MORE POWER");

        if (Request.QueryString["type"]==null)
        {
            CGeneral.GoToErrorPage(Response, "ParamWrong");
        }
        type=Request.QueryString["type"];
        if (type == "notice")
        {
            int uid=int.Parse(Session["uid"].ToString());
            if(Request.QueryString["page"]==null)
                CGeneral.GoToErrorPage(Response, "ParamWrong");

            int page = Check.CheckToInt(Request.QueryString["page"]);
            if(page==-1)
                CGeneral.GoToErrorPage(Response, "ParamWrong");
            List<notices> no = gd.GetUnReadPostNotice(uid, page, pageSize);
            if(no!=null)
            {
                int messegeCount = no.Count;
                int maxPage = messegeCount / pageSize;
                if (messegeCount % pageSize != 0)
                    maxPage = maxPage + 1;
                if (page > maxPage)
                    page = maxPage;
                if (page == 0)
                    page = 1;

                notice.Visible = true;

                if (no != null)
                {
                    foreach (notices n in no)
                    {
                        Literal_notice.Text += generateNotice(n);
                    }
                }
            }

        }
        else if (type == "message")
        {
            message.Visible = true;


        }
        else if (type == "sysmessage")
        {
            int uid = int.Parse(Session["uid"].ToString());
            if (Request.QueryString["page"] == null)
                CGeneral.GoToErrorPage(Response, "ParamWrong");

            int page = Check.CheckToInt(Request.QueryString["page"]);
            if (page == -1)
                CGeneral.GoToErrorPage(Response, "ParamWrong");

            List<notices> no = gd.GetUnReadSystemNotice(uid, page, pageSize);
            if(no!=null)
            {
                int messegeCount = no.Count;
                int maxPage = messegeCount / pageSize;
                if (messegeCount % pageSize != 0)
                    maxPage = maxPage + 1;
                if (page > maxPage)
                    page = maxPage;
                if (page == 0)
                    page = 1;

                sysmessage.Visible = true;
                if (no != null)
                {
                    foreach (notices n in no)
                    {
                        Literal_notice.Text += generateNotice(n);
                    }
                }
            }

        }
        else
            CGeneral.GoToErrorPage(Response, "ParamWrong");

        

    }
}